package com.wugh.fast.kernel.util;

import com.wugh.fast.kernel.core.exception.FastGlobalException;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.StringUtils;

import java.io.UnsupportedEncodingException;

/**
 * 说明:
 *
 * @author wugh
 * @date 2018-10-29 18:29
 */
public class CodecUtils {
    private CodecUtils() {
    }

    public static String encodeBase64(String data) {
        if (data == null) {
            return null;
        }
        try {
            byte[] bytes = Base64.encodeBase64(StringUtils.getBytesUtf8(data));
            return new String(bytes, "utf-8");
        } catch (UnsupportedEncodingException e) {
            throw new FastGlobalException(e);
        }
    }

    public static String decodeBase64(String data) {
        if (data == null) {
            return null;
        }
        try {
            byte[] bytes = Base64.decodeBase64(data.getBytes("utf-8"));
            return new String(bytes, "utf-8");
        } catch (UnsupportedEncodingException e) {
            throw new FastGlobalException(e);
        }
    }
}
